
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox",
], function ($, IO, upui, up, jui, bootbox, definebt) {

    //全局文章article_id,为了修改
    var article_id=0;

    $(function () {

        gettable(1);
        $('#fileupload').fileupload({
            url: '/upload',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            document.getElementById('upimgs').src = '/images/loading.gif';

        }).on('fileuploadprocessalways', function (e, data) {
            //console.log("fileuploadprocessalways",data);
        }).on('fileuploadprogressall', function (e, data) {
            //console.log("fileuploadprogressall",data);
        }).on('fileuploaddone', function (e, data) {
            var alt = $(e.target).attr('alt');
            if (!alt)
                alt = '';
            console.log("#upimgs" + alt);
            $("#upimgs" + alt).attr("src", data._response.result.url);
        });

        $('#fileupload_edit').fileupload({
            url: '/upload',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            document.getElementById('upimgs_edit').src = '/images/loading.gif';

        }).on('fileuploadprocessalways', function (e, data) {
            //console.log("fileuploadprocessalways",data);
        }).on('fileuploadprogressall', function (e, data) {
            //console.log("fileuploadprogressall",data);
        }).on('fileuploaddone', function (e, data) {
            var alt = $(e.target).attr('alt');
            if (!alt)
                alt = '';
            //console.log("#upimgs" + alt);
            $("#upimgs_edit" + alt).attr("src", data._response.result.url);
        });

        $("#btnAddArticle").click(function(){
             $(".article_title").val('');
            $("#upimgs").attr('');
            $(".selectcolumnadd option:selected").attr('');
            editor.html('');
            $(".article_from").val('');
            $('#myModal').modal('show');
        });

        //新增文章
        $(".btnSave").click(function(){

            var article_title= $(".article_title").val();
            var article_pic= $("#upimgs").attr("src");
            var cate_id=$(".selectcolumnadd option:selected").attr('cate_id');
            console.log(cate_id);
            var article_content=editor.html();
            var article_from=$(".article_from").val();

            if(cate_id=="-1"){
                alert('请选择分类');
                return;
            }
            if(article_title.length<=0){
                alert('请输入标题');
                return;
            }
            if(article_pic.length<=0){
                alert('请先上传首图');
                return;
            }
            if(article_content.length<=0){
                alert('请编辑正文');
                return;
            }
            var parmas={};
            parmas.article_title=article_title;
            parmas.article_pic=article_pic;
            parmas.cate_id=cate_id;
            parmas.article_content=article_content;
            parmas.article_from=article_from;
            IO.post('/api/articles/add',parmas,function(d){
                if(d.code==0){
                    $('#myModal').modal('hide');
                    var page=$(".page_control .li_num.selected").text();
                    gettable(page);
                }
                else {
                    alert(d.msg);
                }

            })

        })

        //新增页面的发布文章
        $(".btnPublish").click(function(){

            var article_title= $(".article_title").val();
            var article_pic= $("#upimgs").attr("src");
            var cate_id=$(".selectcolumnadd option:selected").attr('cate_id');
            var article_content=editor.html();
            var article_from=$(".article_from").val();

            if(cate_id=="-1"){
                alert('请选择分类');
                return;
            }
            if(article_title.length<=0){
                alert('请输入标题');
                return;
            }
            if(article_pic.length<=0){
                alert('请先上传首图');
                return;
            }
            if(article_content.length<=0){
                alert('请编辑正文');
                return;
            }
            var parmas={};
            parmas.article_title=article_title;
            parmas.article_pic=article_pic;
            parmas.cate_id=cate_id;
            parmas.article_content=article_content;
            parmas.article_from=article_from;

            IO.post('/api/articles/add',parmas,function(d){
                if(d.code==0){
                    var parmas={};
                    var data=eval("(" + localStorage.getItem('data') + ")");
                    var admin_id=data.data.admin_id;
                    parmas.admin_id=admin_id;
                    parmas.article_publish=1;
                    parmas.article_id=d.data.article_id;

                    IO.post('/api/articles/publish',parmas,function(d){
                        if(d.code==0){
                            bootbox.alert('添加成功');
                            $('#myModal').modal('hide');
                            var page=$(".page_control .li_num.selected").text();
                            gettable(page);
                        }
                    });
                }
                else {
                    alert(d.msg);
                }
            })
        })

        //编辑页面的发布文章
        $(".btnPublish_edit").click(function(){

            var article_title= $(".article_title_edit").val();
            var article_pic= $("#upimgs_edit").attr("src");
            var cate_id=$(".selectcolumnedit option:selected").attr('cate_id');
            var article_content=editor_edit.html();
            var article_from=$(".article_from_edit").val();


            if(cate_id=="-1"){
                alert('请选择分类');
                return;
            }
            if(article_title.length<=0){
                alert('请输入标题');
                return;
            }
            if(article_pic.length<=0){
                alert('请先上传首图');
                return;
            }
            if(article_content.length<=0){
                alert('请编辑正文');
                return;
            }
            var parmas={};
            parmas.article_title=article_title;
            parmas.article_pic=article_pic;
            parmas.cate_id=cate_id;
            parmas.article_content=article_content;
            parmas.article_from=article_from;
            parmas.article_id=article_id;

            //修改
            IO.post('/api/articles/edit',parmas,function(d){
                if(d.code==0){
                    var parmas={};
                    var data=eval("(" + localStorage.getItem('data') + ")");
                    var admin_id=data.data.admin_id;
                    parmas.admin_id=admin_id;
                    parmas.article_publish=1;
                    parmas.article_id=article_id;
                    IO.post('/api/articles/publish',parmas,function(d){

                        if(d.code==0){
                            bootbox.alert('修改成功');
                            $('#myModaledit').modal('hide');
                            var page=$(".page_control .li_num.selected").text();
                            gettable(page);
                        }
                    });
                }
                else {
                    //$('#myModaledit').modal('hide');
                    alert(d.msg);

                }
            })
        });

        //删除文章
        $(".btnDelete").click(function(){

            if(article_id){
                bootbox.confirm("确定要删除吗？",function(result){
                    if(result) {
                        var data=eval("(" + localStorage.getItem('data') + ")");
                        var admin_id=data.data.admin_id;
                        var parmas={};
                        parmas.article_id=article_id;
                        parmas.admin_id=admin_id;

                        IO.post('/api/articles/delete',parmas,function(d){
                            console.log(d);
                            if(d.code==0){
                                bootbox.alert('删除成功');
                                $('#myModaledit').modal('hide');
                                var page=$(".page_control .li_num.selected").text();
                                gettable(page);
                            }else {
                                alert(d.msg);
                            }
                        })
                    }
                });
            }
        });
    });

    function gettable(page){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '标题',
                dataIndex: 'article_title',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].article_title + "</span>";

                }
            },
            {
                title: '栏目',
                dataIndex: 'article_count',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" + data[datarow].catename + "</span>";

                }
            },
            {
                title: '阅读量',
                dataIndex: 'article_count',
                dataSource: function (data, datarow, gridobj, current_column) {

                    return "<span>" + data[datarow].article_count + "</span>";

                }
            },
            {
                title: '状态',
                dataIndex: 'article_publish',
                dataSource: function (data, datarow, gridobj, current_column) {
                    //console.log(data);
                    if(data[datarow].article_publish==1){
                        return "<span>" +"已发布"+ "</span>";
                    }
                    else {
                        return "<span>" +"未发布"+ "</span>";
                    }

                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#'  article_id="+data[datarow].article_id+" class='edit'><span style='color: #6ab7ad;'>" + "编辑" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/articles/list", $("#table_my_toast"), $("#paging_my_toast"), page, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            var cate_id=$(".selectcolumn option:selected").attr("cate_id");
            if(cate_id=="-1"){
                cate_id="";
            }
            var param="cate_id="+cate_id;
            return param;
        }
        dataInit();
    }

    //下拉触发刷新表格,按文章分类查询
    $(document).on('change','.selectcolumn',function(){
        var page=$(".page_control .li_num.selected").text();
        gettable(page);
    });

    //查看详情
    $(document).on('click','.edit',function(){
        article_id=$(this).attr('article_id');
        if(article_id){
            var parmas={};
            parmas.article_id=article_id;
            IO.post('/api/articles/detail',parmas,function(d){
                var data= d;
                //console.log(d);
                $(".article_title_edit").val(data.article_title);
                $("#upimgs_edit").attr("src",data.article_pic);
                $(".selectcolumnedit").val(data.cate_id);
                editor_edit.html(data.article_content);
                $(".article_from_edit").val(data.article_from);
                if(data.article_publish==1){
                    $(".btnPublish_edit").text('修改');
                }
                else {
                    $(".btnPublish_edit").text('修改并发布');
                }
                $('#myModaledit').modal('show');
            });

        }
    })
});

